package com.hrm.controller;

import com.hrm.dao.HrmApartmentMapper;
import com.hrm.dao.HrmPostMapper;
import com.hrm.model.HrmStaff;
import com.hrm.model.HrmUser;
import com.hrm.service.HrmApartmentService;
import com.hrm.service.HrmPostService;
import com.hrm.service.HrmStaffService;
import com.hrm.service.HrmUserService;
import com.hrm.util.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/user")
public class HrmStaffController
{
	@Autowired
	protected HrmStaffService hrmStaffService;
	@Autowired
	protected HrmUserService hrmUserService;
	@Autowired
	protected HrmApartmentService hrmApartmentService;
	@Autowired
	protected HrmPostService hrmPostService;

	/**
	 * @Title: hrmStaffManage
	 * @Description: 打开首页信息页面
	 * @return ModelAndView
	 */
	@RequestMapping(value ="/index.html")
	public ModelAndView index(
			@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize,
			Integer id)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		PageView pageView = new PageView(pageNum);
		pageView.setPageSize(pageSize);

		ModelAndView mav = new ModelAndView("/common/index");

		try
		{
			mav.addObject("apartmentList", hrmApartmentService.getHrmApartment());
			mav.addObject("postList", hrmPostService.getHrmPost());
			mav.addObject("hrmUser", hrmUserService.getHrmUser(id));
			mav.addObject("pageView", hrmStaffService.findEntityPageByParam(pageView, params));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * @Title: hrmStaffManage
	 * @Description: 打开员工信息页面
	 * @return ModelAndView
	 */
	@RequestMapping(value ="/hrmStaffManage.html")
	public ModelAndView hrmStaffManage(
			@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize,
			Integer id, String selectContent)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("selectContent", selectContent);
		PageView pageView = new PageView(pageNum);
		pageView.setPageSize(pageSize);

		ModelAndView mav = new ModelAndView("/hrmstaff");
		System.out.println(hrmApartmentService.getHrmApartment().size());
		System.out.println(hrmPostService.getHrmPost().size());
		try
		{
			mav.addObject("selectContent", selectContent);
			mav.addObject("hrmUser", hrmUserService.getHrmUser(id));
			mav.addObject("apartmentList", hrmApartmentService.getHrmApartment());
			mav.addObject("postList", hrmPostService.getHrmPost());
			mav.addObject("pageView", hrmStaffService.findEntityPageByParam(pageView, params));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 增加员工信息
	 * @param hrmStaff
	 * @return
	 */
	@RequestMapping(value = "/addStaff.post",method = RequestMethod.POST)
	@ResponseBody
	public Integer addStaff(HrmStaff hrmStaff){
		try
		{
			hrmStaffService.addStaff(hrmStaff);
			return 1;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 更新员工信息
	 * @param hrmStaff
	 * @return
	 */
	@RequestMapping(value = "/updateStaffInfo.post",method = RequestMethod.POST)
	@ResponseBody
	public Integer hrmStaffInfoUpdateManage(HrmStaff hrmStaff){
		try
		{
			hrmStaffService.updateStaffInfo(hrmStaff);
			return 1;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 通过id查找员工信息返回数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/hrmStaffId.post",method = RequestMethod.POST)
	public ModelAndView hrmStaffIdManage(Integer id){
		ModelAndView mav = new ModelAndView("/page/staffInfo_update");
		try{
			mav.addObject("hrmStaff",hrmStaffService.getHrmStaff(id));
		}catch (Exception e){
			e.printStackTrace();
		}
		return mav;

	}

	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value ="/deleteByStaffId.post",method = RequestMethod.POST)
	@ResponseBody
	public Integer deleteByStaffId(Integer id){
		try
		{
			hrmStaffService.deleteStaffById(id);
			return 1;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
}
